<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/9/2 0002
 * Time: 上午 12:17
 */
namespace app\dlc\common\createHelper;
use app\dlc\controller\CommonController;
use think\Db;

class CreateController extends Common
{
    public static $path = 'application/dlc/controller/';
    public $table;
    public $edit = '_edit';
    public $list_string;
    public $form_list_data;
    public function create($table,$cover=true){
        $fileNname = $this->camelize($table,1);
        $fields = self::getTableInfo($table);
        $this->table = $table;
        if (!$fields) return;
        $this->phpStart();
        $this->space('namespace app\dlc\controller;
use think\Db;');
        $this->classNameStart("class ".$fileNname."Controller extends CommonController");
        $this->actionStart("public function index()");
        $this->addIndex($table);
        $this->actionEnd();
        if ($this->edit == '_edit'){
            $this->addEdit();
            $this->addDel();
        }else{
            $this->addRoleEidt();
            $this->addRoleDel();
        }

        $this->classNameEnd();        //在类结束之前，要加载一些方法
        $this->phpEnd();
        $this->save(self::$path.$fileNname.'Controller.php',$cover,1);
    }

// ---------------------------   index 方法开始  ------------------------------------------------------------------------
    public function addIndex($table){
        $form_list = model('form_list')->where(['table'=>$table])->find();
        $this->form_list_data = $form_list->data;
        $arr = ['head_botton','head_search','list_botton','list_title'];
        foreach ($arr as $key => $item) {
            if (!empty($form_list[$item])){
                $this->$item($form_list[$item]);
            }
        }
        $this->content('$this->loading();');
        $this->content('$map = $this->map();');
        $this->content('$data = model(CONTROLLER_NAME)->where($map)->order(\'id desc\')->page(self::$page)->select();');
        $this->content('$count = model(CONTROLLER_NAME)->where($map)->count();');
        $this->content('$this->getPage($count, self::$psize, \'App-loader\', $this->title, \'App-search\');');
        $this->content('$this->assign(\'data\',$data);');
        $this->content($this->list_string);
//        $this->content('$list = [\'id\'=>\'ID\',\'name\'=>\'name\',\'ctime\'=>\'创建时间\',\'sys\'=>\'操作\'];');
        $this->content('$this->assign(\'list\',$list);');
        $this->content('echo $this->fetch(\'common/index\');');

    }

    public function head_botton($data){
        $data = explode(',',$data);
        foreach ($data as $key => $datum) {
            $action = 'index_'.$datum;
            $role = $this->getRole('head_botton',$datum);
            if (method_exists($this,$action)){
                $this->$action($role);
            }
        }
    }


    public function head_search($data){
        $data = explode(',',$data);
        foreach ($data as $key => $datum) {
            $name = $this->getName('head_search',$datum);
            $type = 1;
            $role = $this->getRole('head_search',$datum);
            if (strstr($datum,'time')) $type = 3;
            $this->str .= '        $this->headSearch("'.$role.'","'.$datum.'","'.$name.'",'.$type.');'."\r\n";
        }
    }



    public function list_botton($data){
        $data = explode(',',$data);
        foreach ($data as $key => $datum) {
            $action = 'index_'.$datum;
            $role = $this->getRole('list_botton',$datum);
            if (method_exists($this,$action)){
                $this->$action($role);
            }
        }
    }

    public function list_title($data){
        $data = explode(',',$data);
        $this->list_string .= '$list = [';
        $i = 0;
        foreach ($data as $key => $datum) {
            $key_name = $this->getName('list_title',$datum);
            $role = $this->getRole('list_title',$datum);
            $this->list_title_item($role,$datum,$key_name,$i);
            $i++;
        }

        $this->list_string .= ',\'sys\'=>"操作"];';
    }

    public function list_title_item($role = '',$key,$value,$i){
        if ($key == 'status') $key = 'statusText';
        $dou = $i?',':'';
        $role = $role?$role.'-':'';
        $key = $role.$key;
        $this->list_string .= $dou."'$key'=>"."'$value'";
    }


    public function index_back($role = ''){
        $this->str .= '	    $this->headBotton("'.$role.'",\'back\',\'返回\',CONTROLLER_URL . "index");'."\r\n";
    }

    public function index_excel($role = ''){
        $this->str .= '        $this->headBotton("'.$role.'",\'excelDownload\',\'导出\');'."\r\n";
    }

    public function index_check($role = ''){
        $this->str .= '        $this->headBotton("'.$role.'",\'send\',\'勾选并操作\',CONTROLLER_URL . "check",2,"",true);'."\r\n";
        $this->end_action[] = 'addCheck';
    }

    public function index_edit($role = ''){
        if (in_array($this->table,CommonController::$roleList)){
            $this->edit = 'roleEidt';
        }
        $this->str .= '        $this->listBotton("'.$role.'",\'edit\',\'编辑\',CONTROLLER_URL . "'.$this->edit.'");'."\r\n";
    }

    public function index_del($role = ''){
        if ($this->edit == '_edit'){
            $action = '_del';
        }else{
            $action = 'roleDelete';
        }
        $this->str .= '        $this->listBotton("'.$role.'",\'delete\',\'删除\',CONTROLLER_URL . "'.$action.'");'."\r\n";
    }

    public function index_enable($role = ''){
        $this->str .= '        $this->listBotton("'.$role.'",\'enable\',\'启用\',CONTROLLER_URL . \'enable\',1,"","","",[\'status\',\'=\',\'2\']);'."\r\n";
    }

    public function index_disable($role = ''){
        $this->str .= '        $this->listBotton("'.$role.'",\'disable\',\'禁用\',CONTROLLER_URL . \'disable\',1,"","","",[\'status\',\'=\',\'1\']);'."\r\n";
    }


    public function getRole($action,$name){
        $id = $this->table . '-' .$action.'-'.$name;
        return $this->form_list_data[$id]['role']?:'';
    }

    public function getName($action,$name){
        $id = $this->table . '-' .$action.'-'.$name;
        return $this->form_list_data[$id]['title']?:'';
    }





// ---------------------------   index 方法结束  ------------------------------------------------------------------------

    public function addCheck(){
        $this->str .= '    public function check(){
	    $ids = input(\'ids\');
	    admin_return(1,\'勾选并操作成功\');
    }'."\r\n\r\n";
    }

    public function addEdit(){
        $this->str .= '	//$funName 添加和编辑成功后的回调方法,返回 $id,$action , $action =add 是添加，$action = edit 是编辑
    public function _edit($updata = false, $add = false, $noRepeat = false, $funName = false)
    {
//        $updata[\'table\'] = "";//更新的表，默认自动获取
//        $updata[\'key\'] = "";//表的主键，默认自动获取
//        $noRepeat[\'map\'] = "";//新增判断条件
//        $noRepeat[\'name\'] = "";//字段，不可重复的字段
//        $noRepeat[\'tig\'] = "";//提示语
        parent::_edit($updata, $add, $noRepeat, $funName);
    }'."\r\n\r\n";
    }

    public function addRoleEidt(){
        $this->str .= '    //$add_admin = true 新增admin账号。fales  不添加
    public function roleEidt($roleName = false, $add_admin = true)
    {
        parent::roleEidt($roleName, $add_admin);
    }'."\r\n\r\n";
    }

    public function addDel(){
        $this->str .= '    public function _del($tabelName = false, $tableKey = \'id\', $child = false, $delChild = false)
    {
//        $child[\'tableName\'] = \'\';//下级表，如果这个是分类表，使用该分类的表   如:goods 是goods_type的下级
//        $child[\'key\'] = \'\';//外键 如：goods_type_id
//        $child[\'msg\'] = \'该分类下有数据，不能删除\';//提示语
//        $delChild[\'tableName\'] = \'\';///下级表，如果这个是分类表，使用该分类的表，删除该分类的数据 如:goods 是goods_type的下级
//        $delChild[\'key\'] = \'\';//外键 如：goods_type_id
        //$child = [\'tableName\'=>\'shop\',\'key\'=>\'agent_id\',\'msg\'=>\'该分类下有数据，不能删除\'];
        parent::_del($tabelName, $tableKey, $child, $delChild);
    }'."\r\n\r\n";
    }

    public function addRoleDel(){
        $this->str .= '    public function roleDelete($roleName = false, $id = false)
    {
        parent::roleDelete($roleName, $id);
    }'."\r\n\r\n";
    }
}